package com.geeklei.media.ui.menu.popup;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.geeklei.media.ui.R;
import com.geeklei.media.ui.common.AndroidUtils;
import com.geeklei.media.ui.common.PlayerSettings;

public class SettingsPopup extends BaseMenuPopup {
    public static final String TAG = SettingsPopup.class.getName();
    AudioManager mAudioManager;
    private SeekBar mAudioSeekBar;
    private SeekBar mBrightNessSeekBar;
    private Drawable[] mDuokanCodecDrawables;
    private String[] mDuokanCodecValues;
    private boolean mIsAudioEffEnhance = true;
    private boolean mIsFullScreen = false;
    int mMaxVolume;
    private Drawable[] mOriginCodecDrawables;
    private String[] mOriginCodecValues;
    private PlayerSettings mPlayerSettings;
    private SettingAdapter mSettingAdapter;

    public SettingsPopup(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        setTitle(getResources().getString(R.string.vp_select_function));
        this.mPlayerSettings = new PlayerSettings();
        this.mPlayerSettings.init(context);
        this.mIsFullScreen = this.mPlayerSettings.isForceFullScreen();
        this.mIsAudioEffEnhance = this.mPlayerSettings.isAudioEffectOn();
        this.mAudioManager = (AudioManager) ((Activity) getContext()).getSystemService(Context.AUDIO_SERVICE);
        this.mMaxVolume = this.mAudioManager.getStreamMaxVolume(3);
        View inflate = View.inflate(getContext(), R.layout.vp_menu_setting_header, null);
        this.mAudioSeekBar = (SeekBar) inflate.findViewById(R.id.audio_seek_bar);
        this.mAudioSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            public void onStopTrackingTouch(SeekBar seekBar) {
            }

            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            public void onProgressChanged(SeekBar seekBar, int i, boolean z) {
                if (z) {
                    SettingsPopup.this.mAudioManager.setStreamVolume(3, i, 8);
                }
            }
        });
        this.mBrightNessSeekBar = (SeekBar) inflate.findViewById(R.id.brightness_seek_bar);
        this.mBrightNessSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            public void onStopTrackingTouch(SeekBar seekBar) {
            }

            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            public void onProgressChanged(SeekBar seekBar, int i, boolean z) {
                if (z) {
                    AndroidUtils.setActivityBrightness((Activity) SettingsPopup.this.getContext(), i * 17);
                }
            }
        });
        updateAudioAndBrightness();

        this.mSettingAdapter = new SettingAdapter();
        if (this.mListView.getHeaderViewsCount() <= 0) {
            this.mListView.addHeaderView(inflate);
        }
        this.mListView.setAdapter(mSettingAdapter);

//        this.mSettingAdapter.setOnPerformCheckedChangelistener(new OnCheckedChangeListener() {
//            public void onCheckedChanged(CompoundButton compoundButton, boolean z) {
//                int id = compoundButton.getId();
//                if (DuoKanCodecConstants.sUseDuokanCodec) {
//                    if (id == 0) {
//                        SettingsPopup.this.onSwitchVideoWhClicked();
//                    } else if (id == 1 && DuoKanCodecConstants.USE_DIRAC_SOUND) {
//                        SettingsPopup.this.onAudioEffectClicked();
//                    }
//                } else if (id == 0) {
//                    SettingsPopup.this.onSwitchVideoWhClicked();
//                }
//            }
//        });
//        this.mListView.setAdapter(this.mSettingAdapter);
    }

    public void show(ViewGroup viewGroup) {
        super.show(viewGroup);
        updateAudioAndBrightness();
//        refreshListView();
    }

//    private void setupValues() {
//        if (DuoKanCodecConstants.USE_DIRAC_SOUND) {
//            this.mDuokanCodecValues = new String[]{getContext().getResources().getString(R.string.vp_scale_screen), getContext().getResources().getString(R.string.menu_item_enable_audio_enhance)};
//            this.mDuokanCodecDrawables = new Drawable[]{getContext().getResources().getDrawable(R.drawable.play_full_icon), getContext().getResources().getDrawable(R.drawable.play_sound_icon)};
//        } else {
//            this.mDuokanCodecValues = new String[]{getContext().getResources().getString(R.string.vp_scale_screen)};
//            this.mDuokanCodecDrawables = new Drawable[]{getContext().getResources().getDrawable(R.drawable.play_full_icon)};
//        }
//        this.mOriginCodecValues = new String[]{getContext().getResources().getString(R.string.vp_scale_screen)};
//        this.mOriginCodecDrawables = new Drawable[]{getContext().getResources().getDrawable(R.drawable.play_full_icon)};
//    }

    private void updateAudioAndBrightness() {
        int streamVolume = this.mAudioManager.getStreamVolume(3);
        this.mAudioSeekBar.setMax(this.mMaxVolume);
        this.mAudioSeekBar.setProgress(streamVolume);
        streamVolume = (int) (AndroidUtils.getActivityBrightness((Activity) getContext()) * 255.0f);
        if (streamVolume < 0) {
            streamVolume = AndroidUtils.getSystemBrightness(getContext());
        }
        this.mBrightNessSeekBar.setMax(15);
        this.mBrightNessSeekBar.setProgress(streamVolume / 17);
    }

    private void onAudioEffectClicked() {
        this.mIsAudioEffEnhance = !this.mIsAudioEffEnhance;
        this.mPlayerSettings.setAudioEffect(this.mIsAudioEffEnhance);
//        SoundEffect.turnOnMovieMode(this.mIsAudioEffEnhance);
//        refreshListView();
    }

    private void onSwitchVideoWhClicked() {
        this.mIsFullScreen = !this.mIsFullScreen;
        this.mPlayerSettings.setForeceFullScreen(this.mIsFullScreen);
//        if (this.mVideoView != null) {
//            this.mVideoView.setForceFullScreen(this.mIsFullScreen);
//        }
    }

//    private void refreshListView() {
//        this.mSettingAdapter.setGroup(getAdapterValues(), getAdapterDrawables(), getSettingSelected());
//    }

//    private String[] getAdapterValues() {
//        setupValues();
//        if (DuoKanCodecConstants.sUseDuokanCodec) {
//            return this.mDuokanCodecValues;
//        }
//        return this.mOriginCodecValues;
//    }
//
//    private Drawable[] getAdapterDrawables() {
//        setupValues();
//        if (DuoKanCodecConstants.sUseDuokanCodec) {
//            return this.mDuokanCodecDrawables;
//        }
//        return this.mOriginCodecDrawables;
//    }
//
//    private boolean[] getSettingSelected() {
//        if (DuoKanCodecConstants.sUseDuokanCodec && DuoKanCodecConstants.USE_DIRAC_SOUND) {
//            return new boolean[]{this.mIsFullScreen, this.mIsAudioEffEnhance};
//        } else if (DuoKanCodecConstants.sUseDuokanCodec) {
//            return new boolean[]{this.mIsFullScreen};
//        } else {
//            return new boolean[]{this.mIsFullScreen};
//        }
//    }

    protected int getPopupWidth() {
        return getContext().getResources().getDimensionPixelSize(R.dimen.vp_menu_popup_settings_width);
    }

    public boolean needPauseVideo() {
        return false;
    }
}
